package com.example.autoemail;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;

/**
* 2014/8/28  9:57
* Created by YANG
*
*/
public class EmailView extends AutoCompleteTextView {
    //邮箱数组
    String[] emailArray = {"@163.com", "@126.com", "@qq.com", "@sina.com", "@yahoo.com", "@hotmail.com"};
    private Context mContext;
    private MyAdapter adapter;

    public EmailView(Context context) {
        super(context);
        shareConstructor(context);

    }

    public EmailView(Context context, AttributeSet attrs) {
        super(context, attrs);
        shareConstructor(context);

    }

    public EmailView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        shareConstructor(context);
    }

    private void shareConstructor(Context context) {
        adapter = new MyAdapter(context);
        setThreshold(1);
        setAdapter(adapter);
        mContext = context;
        addTextChangedListener(new MyTextWatcher());
    }

    public void setAdapter(MyAdapter adapter) {
        if (!(adapter instanceof MyAdapter)) {
            throw new RuntimeException("You cant use custom Adapter in this class!!!");
        }
        super.setAdapter(adapter);
    }

    class MyTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String input = s.toString();
            adapter.mList.clear();
            if (input.length() > 0) {
                for (int i = 0; i < emailArray.length; i++) {
                    if (input.contains("@")) {
                        String filter = input.substring(input.indexOf("@") + 1, input.length());
                        if (emailArray[i].contains(filter)) {
                            adapter.mList.add(input.substring(0, input.indexOf("@")) + emailArray[i]);
                        }
                    } else {
                        adapter.mList.add(input + emailArray[i]);
                    }

                }
            }
            adapter.notifyDataSetChanged();
            showDropDown();
        }
    }

}

class MyAdapter extends BaseAdapter implements Filterable {
    List<String> mList;
    private Context mContext;
    private MyFilter mFilter;

    public MyAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<String>();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList == null ? null : mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.dropdown_item, null);
        }
        TextView tv = (TextView) view.findViewById(R.id.text1);
        tv.setText(mList.get(position));
        return view;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new MyFilter();
        }
        return mFilter;
    }

    private class MyFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (mList == null) {
                mList = new ArrayList<String>();
            }
            results.values = mList;
            results.count = mList.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}